<?php

/**
 * Description of Catalog
 *
 * @author Kam
 */
class Application_Model_DAO_Catalog extends My_DAO_Abstract {

    const DB_STATIC_PAGE = 'Catalog';
    const STEP_KEY = 'step';
    const STEP_LOGIN = 'step-login';
    const STEP_CATEGORY = 1;
    const STEP_TYPE = 2;
    const STEP_FORM = 3;
    const STEP_PREVIEW = 5;
    const STEP_PAYMENT = 6;
    const STEP_PUBLISH = 7;
    const TYPE_FREE = 'free';
    const TYPE_PAID = 'paid';
    const PHOTOS_FREE_AMOUNT = 0;
    const PHOTOS_PAID_AMOUNT = 1;

    /**
     * 
     * @return Zend_Db_Table_Select
     */
    public function getFullEntries() {
        $select = $this->getDbTable()->select()
                ->from(array('c' => 'catalog'), array('offer_code' => 'code', '*'))
                ->joinLeft(array('cc' => 'catalog_category'), 'c.catalog_category_id=cc.catalog_category_id', array('category_code' => 'code', 'category_admin_active' => 'admin_active', 'parent_id', 'level', 'order_id', 'path', 'name', 'description', 'icon_path', 'meta_desc', 'meta_keyword'))
                ->joinLeft(array('p' => 'payment'), 'c.catalog_id=p.source_id AND p.source_name="catalog"', array('*'))
                ->order(array('c.catalog_id DESC'))
        ;
        return $select;
    }

    public function deactivate($id, $column = 'code') {
        return $this->getDbTable()->update(array('admin_active' => 0), $id, $column);
    }

    public function activate($id, $column = 'code') {
        return $this->getDbTable()->update(array('admin_active' => 1), $id, $column);
    }

    public function getShortCatalogList($limit = 5) {
        /* @var $select Zend_Db_Select */
        $select = $this->getDbTable()->selectActiveEntries();
        $select->limit($limit);
        return $select->query()->fetchAll();
    }

    public function validatePhotos(Zend_Session_Namespace $bean) {
        $type = $bean->type;
        $photos = $bean->photos;
        $cnt = count($photos);
        if ($type == self::TYPE_PAID) {
            if ($cnt >= self::PHOTOS_PAID_AMOUNT) {
                return $this->translate('XXX_DAO_CATALOG_INVALID_PHOTO_NUMBER_PAID');
            }
        } else {
            if ($cnt >= self::PHOTOS_FREE_AMOUNT) {
                return $this->translate('XXX_DAO_CATALOG_INVALID_PHOTO_NUMBER_FREE');
            }
        }
        return true;
    }

    public function update($catalogId, array $data) {
        return $this->getDbTable()->update($data, $catalogId);
    }

    public function addNew($formData, $data) {
        $type = $data['type'];
        $date = date(My_Db_Table::DATE_TIME_PATTERN);
        $catalogRecord = array(
            'author_id' => $data['author_id'],
            'catalog_category_id' => $formData['catalog_category_id'],
            'type' => $type,
            'active' => isset($data['active']) ? $data['active'] : ($type == 'free' ? 1 : 0),
        );
        $this->setSafety($catalogRecord, $formData, 'title');
        $this->setSafety($catalogRecord, $formData, 'address');
        $this->setSafety($catalogRecord, $formData, 'city');
        $this->setSafety($catalogRecord, $formData, 'zip_code');
        $this->setSafety($catalogRecord, $formData, 'phone');
        $this->setSafety($catalogRecord, $formData, 'email');
        $this->setSafety($catalogRecord, $formData, 'web');
        $this->setSafety($catalogRecord, $formData, 'description');

        $catalogId = $this->getDbTable()->insert($catalogRecord);

        $catalogRecord = $this->getDbTable()->get($catalogId);
        return $catalogRecord;
    }

    public function getCatalogByCode($code) {
        /* @var $select Zend_Db_Select */
        $select = $this->getDbTable()->selectActiveEntries();
        $catalog = $select->where('c.code = ?', $code)
                        ->query()->fetch();
        if(empty($catalog)) {
            return null;
        }
        $catalogId = $catalog['catalog_id'];
        $modelImages = new Application_Model_DbTable_Image();
        $images = $modelImages->getBySource('catalog', $catalogId)
                        ->query()->fetchAll();
        $catalog['images'] = $images;
        return $catalog;
    }

    /**
     * 
     * @param int $categoryId
     * @param int $page
     * @param int $rowCount
     * @return Zend_Db_Select
     */
    public function getByCategoryId($categoryId) {
        $daoCategory = new Application_Model_DAO_CatalogCategory();
        $subCategoriesIds = $daoCategory->getSubCategoriesIds($categoryId);

        /* @var $select Zend_Db_Select */
        $select = $this->getDbTable()->selectActiveEntries();
        $select->where('c.catalog_category_id IN (?)', array_merge(array($categoryId), $subCategoriesIds));
        return $select;
    }

    public function getCatalogSimilars($catalog, $rowCount) {
        $select = $this->getDbTable()->selectActiveEntries();
        $select->where('c.catalog_id != ?', $catalog['catalog_id']);
        $select->where('c.catalog_category_id = ?', $catalog['catalog_category_id'])
                ->limitPage(1, $rowCount);
        $catalogs = $select->query()->fetchAll();
        return $catalogs;
    }
    
    /**
     *
     * @param type $userId
     * @return type 
     */
    public function getCatalogsByAuthor($userId){
        $select = $this->getFullEntries();
        $select->where('c.created_by = ?', $userId);
        return $select;
    }

    protected function getDbTableNameMap() {
        return array(
            self::DB_STATIC_PAGE => 'Application_Model_DbTable_Catalog',
        );
    }

    protected function getDefaultDbTableName() {
        return self::DB_STATIC_PAGE;
    }

}
